﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PerfumeShopManagement.Model;
using PerfumeShopManagement.DAL;
using System.Data.Linq;
using PerfumeShopManagement.Utility;
using System.Data.Objects.DataClasses;

namespace PerfumeShopManagement.BLL
{
    public class BllExportCheck : BllAbstractBase<ExportCheck, int>
    {
        public BllExportCheck() 
            : base()
        {
            items = DalFactory.ExportCheckDAL.GetAllObjects();
        }

        public override List<ExportCheck> GetAll()
        {
            return items;
        }

        public override ExportCheck Get(int id)
        {
            foreach (ExportCheck exportCheck in items)
            {
                if (exportCheck.ExportCheckId == id)
                {
                    return exportCheck;
                }
            }

            return null;
        }

        public override List<ExportCheck> Search(ExportCheck criteria)
        {
            throw new NotImplementedException();
        }

        public override bool Save(ExportCheck obj)
        {
            ExportCheck exportCheck = obj;

            // Megre amount
            EntityCollection<ExportCheckDetail> exportCheckDetails = exportCheck.ExportCheckDetail;
            for (int i = exportCheckDetails.Count - 1; i >= 0; i--)
            {
                for (int j = i - 1; j >= 0; j--)
                {
                    ExportCheckDetail exI = exportCheckDetails.ElementAtOrDefault<ExportCheckDetail>(i);
                    ExportCheckDetail exJ = exportCheckDetails.ElementAtOrDefault<ExportCheckDetail>(j);

                    if (exI.ProductId == exJ.ProductId)
                    {
                        exJ.ExportCheckAmount += exI.ExportCheckAmount;
                        exportCheckDetails.Remove(exI);
                        break;
                    }
                }
            }

            bool updated = false;
            foreach (ExportCheck item in items)
            {
                if (item.ExportCheckId == exportCheck.ExportCheckId)
                {
                    item.ExportCheckTime = exportCheck.ExportCheckTime;
                    item.Username = exportCheck.Username;
                    item.ExportCheckDetail = exportCheck.ExportCheckDetail;
                    item.Total = exportCheck.Total;
                    updated = true;
                    DalFactory.ExportCheckDAL.Update(exportCheck);

                    return true;
                }
            }

            if (!updated)
            {
                items.Add(exportCheck);
                DalFactory.ExportCheckDAL.Insert(exportCheck);

                return true;
            }

            return false;
        }

        public override bool DeleteById(int id)
        {
            if (DalFactory.ExportCheckDAL.DeleteById(id))
            {
                for (int i = items.Count - 1; i >= 0; i--)
                {
                    if (id == items[i].ExportCheckId)
                        items.Remove(items[i]);
                }
                return true;
            }
            return false;
        }

        public void ProcessAllNewOrders()
        {
            foreach (ExportCheck item in items)
            {
                if (item.Status == Constants.NEW_STATUS)
                {
                    if (isEnoughToMeet(item))
                    {
                        foreach (ExportCheckDetail detail in item.ExportCheckDetail)
                        {
		                    Product product = DalFactory.ProductDAL.GetObjectById(detail.ProductId);
                            product.Inventory -= detail.ExportCheckAmount;
                            DalFactory.ProductDAL.Update(product);

                            RepositoryHistory history = new RepositoryHistory();
                            history.ProductId = detail.ProductId;
                            history.ActivityKind = Constants.EXPORT_KIND;
                            history.ActiveTime = DateTime.Now;
                            history.Value = detail.ExportCheckAmount;
                            DalFactory.RepositoryHistoryDAL.Insert(history);
                        }

                        item.Status = Constants.COMPLETED_STATUS;
                    }
                    else
                    {
                        item.Status = Constants.PENDING_STATUS;
                    }
                    // Updates status
                    Save(item);
                }
            }
        }
        
        private bool isEnoughToMeet(ExportCheck exportCheck) {
            // Checks if the current repository is enough.
            foreach (ExportCheckDetail detail in exportCheck.ExportCheckDetail)
            {
                Product product = DalFactory.ProductDAL.GetObjectById(detail.ProductId);
                if (product.Inventory < detail.ExportCheckAmount)
                    return false;
            }
            return true;
        }
    }
}
